<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\MobileDeviceRequest;
use App\Models\MobileDevice;
use App\Transformers\MobileDeviceTransformer;
use Illuminate\Http\Request;

class MobileDeviceController extends Controller
{
    public function index(MobileDeviceRequest $request)
    {
        $devices_token = $request->get('devices_token');

        $data = MobileDevice::where('devices_token', $devices_token)->first();

        if (!$data) {
            $this->response->error(trans('api.not_exists'), 200);
        }

        return $this->response->item($data, new MobileDeviceTransformer());
    }

    public function updateOrCreate(MobileDeviceRequest $request)
    {
        $devices_token = $request->get('devices_token');

        $data = $request->only([
            'devices_sys', 'language', 'exchange_rate_id',
        ]);

        $res = MobileDevice::updateOrCreate([
            'devices_token' => $devices_token,
        ], $data);

        if ($res) {
            return response()->json(['message' => trans('api.save_succeed'), 'code' => 1, 'status' => true, 'data' => ''], 200);
        } else {
            return response()->json(['message' => trans('api.save_failed'), 'code' => 0, 'status' => false], 200);
        }
    }
}
